//
//  SafetyViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2018/12/28.
//  Copyright © 2018 xiaohan. All rights reserved.
//

#import "SafetyViewController.h"
#import "MineSwitchItem.h"
#import "SetSafetyCodeViewController.h"
@interface SafetyViewController ()

@end

@implementation SafetyViewController
- (instancetype)init
{
    return [super initWithStyle:UITableViewStylePlain];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    MineSwitchItem *item = (MineSwitchItem *)[[self.sections firstObject].items firstObject];
    item.open = IS_DIDSET_LOCAl_Application_Key;
    MineSwitchItem *item1 = (MineSwitchItem *)[[self.sections firstObject].items objectAtIndex:2];
    item1.open = kIsNeedBackgroundLockApplication;
    MineSwitchItem *item2 = (MineSwitchItem *)[[self.sections firstObject].items objectAtIndex:3];
    item2.open = kIsNeedFaceIDOrTouchIDUnLock;
    [self.tableView reloadData];
}

- (void)setupUI{
    MineSwitchItem *item0 = [MineSwitchItem itemWithTitle:XLocalized(@"应用密码") isOpen:IS_DIDSET_LOCAl_Application_Key];
    item0.cellHeight = 60.0f;
    kWeakSelf(self);
    item0.SwitchBlock = ^(MineSwitchItem *item) {
        DLog(@"选择%d",item.open);
        if (item.open) {
            SetSafetyCodeViewController *vc = [[SetSafetyCodeViewController alloc]initWithSafetyCodeType:SafetyCodeTypeSet];
            [weakself.navigationController pushViewController:vc animated:YES];
        }else{
            kUnNeedBackgroundLockApplication;
            kRemovNeedFaceIDOrTouchIDUnLock;
            [weakself.navigationController presentViewController:[[RootNavigationController alloc]initWithRootViewController:[SetSafetyCodeViewController validationSafetyType:SafetyCodeTypeRemove codeCallback:^(SetSafetyCodeViewController *codeVc , BOOL success) {
                [codeVc backBtnClicked];
            } noDismiss:NO]] animated:YES completion:nil];
        }
    };
    LMJWordArrowItem *item1 = [LMJWordArrowItem itemWithTitle:XLocalized(@"修改密码") subTitle: @""];
    item1.cellHeight = 60.0f;
    item1.itemOperation = ^(NSIndexPath *indexPath) {
        SetSafetyCodeViewController *vc = [[SetSafetyCodeViewController alloc]initWithSafetyCodeType:SafetyCodeTypeChange];
        [weakself.navigationController pushViewController:vc animated:YES];
    };
    MineSwitchItem *item2 = [MineSwitchItem itemWithTitle:XLocalized(@"进入后台立即锁定") isOpen:kIsNeedBackgroundLockApplication];
    item2.SwitchBlock = ^(MineSwitchItem *item) {
        if (item.open) {
            kNeedBackgroundLockApplication;
        }else{
            kUnNeedBackgroundLockApplication;
        }
    };
    item2.cellHeight = 60.0f;
    MineSwitchItem *item3 = [MineSwitchItem itemWithTitle:kIsIphoneX?XLocalized(@"面容解锁"):XLocalized(@"指纹解锁") isOpen:kIsNeedFaceIDOrTouchIDUnLock];
    item3.cellHeight = 60.0f;
    item3.SwitchBlock = ^(MineSwitchItem *item) {
        if (item.open) {
            kSetNeedFaceIDOrTouchIDUnLock;
        }else{
            kRemovNeedFaceIDOrTouchIDUnLock;
        }
    };
    
    LMJItemSection *section = [LMJItemSection sectionWithItems:@[item0,item1,item2,item3] andHeaderTitle:nil footerTitle:nil];
    [self.sections addObject:section];
    self.tableView.tableFooterView = [UIView new];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return IS_DIDSET_LOCAl_Application_Key?4:1;
}

@end
